home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / expand.c < prev    next >
C/C++ Source or Header  |  1998-06-13  |  9KB  |  386 lines

  1. /*
  2. *                               expand.c
  3. *
  4. * Expands a msg file.  V.1.2
  5. */
  6. #include "ctdl.h"    /* header file  */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <ctype.h>
  12. #include <time.h>
  13. #include <proto/exec.h>
  14. #include <pragmas/dos_pragmas.h>
  15. #include "exec/memory.h"
  16. #include "exec/ports.h"
  17. #include "exec/exec.h"
  18. #include "dos/dos.h"
  19. extern struct DosLibrary *DOSBase ;
  20.  
  21. /*
  22. *                               history
  23. *
  24. * 85Nov16 HAW  Modified for MS-DOS.
  25. * 85Apr22 HAW  Move to MS-DOS.
  26. * 84Dec09 HAW  Now merely expands a file, rather than copying & expanding.
  27. * 84Jun22 HAW  Version 1.1 created -- handles any file expansion.
  28. * 84Jun19 HAW  Created.
  29. */
  30. /*
  31. *                               contents
  32. *
  33. *       copy_remainder()        Copies remainder of split msg into file
  34. *       crashout()              irrecoverable error
  35. *       first_part()            Remembers remainder of split msg
  36. *       getUtilNumber()         prompt user for a number, limited range
  37. *       getUtilString()         gets a string from the console
  38. *       main()                  Main controller for this program
  39. */
  40. BPTR    Amsgfl;
  41. extern FILE *upfd;
  42. FILE *netLog = stdout;
  43. char            split_buffer[10000];
  44. extern          CONFIG cfg;
  45.  
  46. char mode[6];
  47.  
  48. unsigned copy_remainder ( unsigned offset );
  49. void crashout ( char *str );
  50. int first_part ( void );
  51. int getUtilNumber ( char *prompt , unsigned bottom , unsigned top );
  52. void getUtilString ( char *prompt , char *buf , int lim );
  53. void Expand_InitMsgBase ( void );
  54. void SopenFile ( char *filename , BPTR *fd );
  55. long myseek ( BPTR fd , long rpos , int mode );
  56. BPTR myopen ( char *name , char *mode );
  57. void myclose ( BPTR fd );
  58. long myread ( void *buff , int size , int blocks , BPTR fp );
  59. long mywrite ( void *buff , int size , int blocks , BPTR fp );
  60.  
  61. int NEUtilGetch(void);
  62. int UtilGetch(void);
  63. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  64.  
  65. /*
  66. * copy_remainder()
  67. *
  68. * This copies the remainder of the split msg onto the "end" of the file.
  69. */
  70. unsigned copy_remainder(offset)
  71. unsigned offset;
  72.   {
  73.   unsigned char *beg;
  74.   int  count, i;
  75.   beg = split_buffer;
  76.   count = 0;
  77.   printf("Transfer %d sectors to cover split msg\n",
  78.   (offset) ? offset / MSG_SECT_SIZE + 1 : 0);
  79.   while (1)
  80.     {
  81.     if (offset < MSG_SECT_SIZE) break;
  82.     crypte(beg + (MSG_SECT_SIZE * count), MSG_SECT_SIZE, 0);
  83.     if (mywrite(beg + (MSG_SECT_SIZE * count), MSG_SECT_SIZE, 1, Amsgfl) != 1)
  84.       {
  85.       printf("\ncopy_remainder:Error writing first set %d aborting\n",count);
  86.       exit(100);
  87.       };
  88.     count++;
  89.     offset -= MSG_SECT_SIZE;
  90.  
  91.     }
  92.   if (offset == 0) return (unsigned)count;
  93.   for (i = 0; beg[MSG_SECT_SIZE * count + i] != 255; i++) ;
  94.   for (i++; i < MSG_SECT_SIZE; i++)
  95.   beg[MSG_SECT_SIZE * count + i] = 0;
  96.   crypte(beg + MSG_SECT_SIZE * count, MSG_SECT_SIZE, 0);
  97.   if (mywrite(beg + (MSG_SECT_SIZE * count), MSG_SECT_SIZE, 1, Amsgfl) != 1)
  98.     {
  99.     printf("\ncopy_remainder:Error writing second set %d aborting\n",count);
  100.     exit(100);
  101.     };
  102.   return (unsigned ) (count + 1);
  103.  
  104.   }
  105. /*
  106. * crashout()
  107. *
  108. * This handles the irrecoverable error.
  109. */
  110. void crashout(str)
  111. char *str;
  112.   {
  113.   printf(str);
  114.   exit(100);
  115.  
  116.   }
  117. /*
  118. * first_part()
  119. *
  120. * This remembers the remainder of first msg.
  121. */
  122. int first_part()
  123.   {
  124.   int  offset, i;
  125.   DATA_BLOCK buf;
  126.   char done;
  127.   offset = 0;
  128.   done   = FALSE;
  129.   do
  130.     {
  131.     if (myread(buf, MSG_SECT_SIZE, 1, Amsgfl) != 1)
  132.       {
  133.       printf("\nError on read first message\n");
  134.       exit(100);
  135.       };
  136.     crypte(buf, MSG_SECT_SIZE, 0);
  137.     i = 0;
  138.     while (buf[i] != 255 && i < MSG_SECT_SIZE)
  139.     split_buffer[offset++] = buf[i++];
  140.     done = !(i == MSG_SECT_SIZE);
  141.  
  142.     }
  143.   while (!done);
  144.   myseek(Amsgfl, 0, SEEK_SET);  /* rewind to begining */
  145.   return offset;
  146.  
  147.   }
  148. /*
  149. * getUtilNumber()
  150. *
  151. * This function prompts for a number in (bottom, top) range.
  152. */
  153. int getUtilNumber(prompt, bottom, top)
  154. char   *prompt;
  155. unsigned bottom;
  156. unsigned top;
  157.   {
  158.   unsigned try;
  159.   char numstring[NAMESIZE];
  160.   do
  161.     {
  162.     getUtilString(prompt, numstring, NAMESIZE);
  163.     try     = atoi(numstring);
  164.     if (try < bottom)  printf("Sorry, must be at least %d\n", bottom);
  165.     if (try > top   )  printf("Sorry, must be no more than %d\n", top);
  166.  
  167.     }
  168.   while ((try < bottom ||  try > top));
  169.   return (int) try;
  170.  
  171.   }
  172. /*
  173. * getUtilString()
  174. *
  175. * This will get a string from the user.
  176. */
  177. void getUtilString(prompt, buf, lim)
  178. char *prompt;
  179. char *buf;
  180. int  lim;       /* max # chars to read */
  181.   {
  182.   char c;
  183.   int  i;
  184.   if (strLen(prompt) > 0)
  185.     {
  186.     printf("\nEnter %s\n : ", prompt, lim);
  187.  
  188.     }
  189.   i   = 0;
  190.   while (
  191.   c = NEUtilGetch(),
  192.   c     != NEWLINE
  193.   && c  != '\r'
  194.   && i     <  lim
  195.   )
  196.     {
  197.     /* handle delete chars: */
  198.     if (c == BACKSPACE)
  199.       {
  200.       putchar(' ');
  201.       putchar(BACKSPACE);
  202.       if (i > 0) i--;
  203.       else
  204.         {
  205.         putchar(' ');
  206.         putchar(BELL);
  207.  
  208.         }
  209.  
  210.       }
  211.     else
  212.       {
  213.       putchar(c);
  214.       buf[i++] = c;
  215.  
  216.       }
  217.     if (i >= lim)
  218.       {
  219.       putchar(BELL);
  220.       putchar(BACKSPACE); i--;
  221.  
  222.       }
  223.  
  224.     }
  225.   buf[i]  = '\0';
  226.  
  227.   }
  228. /*
  229. * main()
  230. *
  231. * This is the main controller.
  232. */
  233. int  main(void);
  234. int main()
  235.   {
  236.   char     temp[MSG_SECT_SIZE];
  237.   char c;
  238.   int      i, offset,cnt;
  239.   unsigned oldsize, fudge;
  240.   long work;
  241.   strcpy(mode,"rb+");
  242.   printf("Citadel Message Data File Expander %s \n%s\n", VERSION_NAME, COPYRIGHT);
  243.   printf("Have you backed up the ctdlmsg.sys file yet (y/n)? ");
  244.   c = getchar();
  245.   while( getchar() != '\n');
  246.   if ( c != 'Y' && c != 'y' )
  247.     {
  248.     printf("Please do so before proceeding.  Failure to do so may\n");
  249.     printf("result in you loosing everything...\n");
  250.     exit(1);
  251.  
  252.     }
  253.   printf("\n\nOne moment, please...\n");
  254.   cfg.weAre = UTILITY;
  255.   if (readSysTab(TRUE, TRUE))
  256.     {
  257.     if (access(LOCKFILE, 0) != -1)
  258.       {
  259.       printf("Please do not run Expand using Outside Commands.\n");
  260.       writeSysTab();
  261.       exit(1);
  262.  
  263.       }
  264.     cfg.weAre = UTILITY;
  265.     Expand_InitMsgBase();
  266.     oldsize     = cfg.maxMSector;
  267.     printf("\nOld size was %dK", cfg.maxMSector / (1024 / MSG_SECT_SIZE));
  268.     cfg.maxMSector = getUtilNumber("\nNew size (in decimal!)",
  269.     (unsigned) cfg.maxMSector/(1024/MSG_SECT_SIZE), 65535);
  270.     cfg.maxMSector *= (1024 / MSG_SECT_SIZE);
  271.     printf("\nThank you.  Working...");
  272.     offset = first_part();
  273.     writeSysTab();      /* Restore again, don't have to reconfigure */
  274.     work = oldsize;
  275.     work *= MSG_SECT_SIZE;
  276.     myseek(Amsgfl, 0,SEEK_END); /* Get to EOF */
  277.     cnt = 0;
  278.     printf("Starting the expansion\n");
  279.     while (myread(temp, MSG_SECT_SIZE, 1, Amsgfl) == 1)
  280.       {
  281.       printf("reading %08.8d\r",++cnt);
  282.       };
  283.     printf("\n");
  284.     fudge = copy_remainder(offset);
  285.     for (i = 0; i < MSG_SECT_SIZE; i++) temp[i] = 0;
  286.     crypte(temp, MSG_SECT_SIZE, 0);
  287.     i = cfg.maxMSector - oldsize - fudge;
  288.     printf("And now %7d sectors left to initialize\n", i);
  289.  
  290.     for (; i; i--)
  291.       {
  292.       printf("%7d\r", i);
  293.       if (mywrite(temp, MSG_SECT_SIZE, 1, Amsgfl) != 1)
  294.         {
  295.         printf("****Error writing expansion sectors(%d left to do)\n",i);
  296.         exit(100);
  297.         };
  298.       };
  299.     myclose(Amsgfl);
  300.     printf("\nFinished.  Don't need to reconfigure!  But don't forget");
  301.     printf(" to change CTDLCNFG.SYS.\n ");
  302.  
  303.     }
  304.   return 0;
  305.   }
  306. /*
  307. * InitMsgBase()
  308. *
  309. * This function opens the msg base(s), inits the msg buffers.
  310. */
  311. void Expand_InitMsgBase()
  312.   {
  313.   SYS_FILE name;
  314.   makeSysName(name, "ctdlmsg.sys", &cfg.msgArea);
  315.   SopenFile(name, &Amsgfl);
  316.   InitBuffers();
  317.  
  318.   }
  319. /*
  320. * SopenFile()
  321. *
  322. * This opens one of the .sys files.   Has extra parameter for mode
  323. * which is global.
  324. */
  325. void SopenFile(char *filename, BPTR *fd)
  326.   {
  327.  
  328.   /* We use fopen here rather than safeopen for link reasons */
  329.   if ((*fd = myopen(filename, mode)) == NULL)
  330.     {
  331.     printf("?no %s", filename);
  332.     exit(SYSOP_EXIT);
  333.  
  334.     }
  335.  
  336.   }
  337.  
  338. long myseek( BPTR fd, long rpos, int mode)
  339.   {
  340.  
  341.   switch (mode )
  342.     {
  343.     case SEEK_SET: mode = OFFSET_BEGINNING;break;
  344.     case SEEK_CUR: mode = OFFSET_CURRENT  ;break;
  345.     case SEEK_END: mode = OFFSET_END      ;break;
  346.     default:
  347.     fprintf(stderr,"Error Invalid fseek mode:%d\n",mode);
  348.     return -1;
  349.     };
  350.  
  351.   if( fd == NULL )
  352.     {
  353.     fprintf(stderr, "Error NULL file pointer\n");
  354.     return -1;
  355.     };
  356.   (void)Seek( fd, rpos, mode );
  357.   return 0;
  358.   }
  359.  
  360. BPTR myopen( char *name, char *mode )
  361.   {
  362.   return Open( name, MODE_OLDFILE);
  363.   }
  364.  
  365. void myclose(BPTR fd)
  366.   {
  367.   Close(fd);
  368.   }
  369.  
  370. long  myread(void *buff, int size, int blocks, BPTR fp    )
  371.   {
  372.   long rsize, isize;
  373.   rsize = size * blocks;
  374.   isize =  Read( fp, buff, rsize  );
  375.   return (isize/size);
  376.   }
  377.  
  378.  
  379. long  mywrite(void *buff, int size, int blocks, BPTR fp    )
  380.   {
  381.   long rsize, isize;
  382.   rsize = size * blocks;
  383.   isize =  Write( fp, buff, rsize  );
  384.   return (isize/size);
  385.   }
  386.